<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>表单验证</title>
    <script>
        function validateForm() {
            var x = document.forms["myForm"]["fname"].value;
            if (x == "") {
                alert("必须填写姓名");
                return false;
            }
        }
    </script>
</head>
<body>
<form name="myForm" action="/demo/action_page.php" onsubmit="return validateForm()" method="post">
    姓名：<input type="text" name="fname" required>
    <input type="submit" value="提交">
</form>
</body>
</html>